package com.rocky.pubimport.utils;

import java.io.File;

/**
 * 文件工具类
 * @author rocky
 * @date 2022/4/13 17:11
 */
public final class FileUtils {

    public static String getDirectoryName(String path) {
        int index = path.lastIndexOf(File.separatorChar);
        return index != -1 ? path.substring(0, index) : null;
    }

    public static String getFileName(String path) {
        String fileNameWithExtension = path.substring(path.lastIndexOf(File.separatorChar) + 1);
        int index = fileNameWithExtension.lastIndexOf(46);
        return index != -1 ? fileNameWithExtension.substring(0, index) : fileNameWithExtension;
    }

    public static String getFileExtension(String path) {
        int index = path.lastIndexOf(46);
        return index != -1 ? path.substring(index) : "";
    }

    public static File createDirectoryIfNecessary(String path) {
        if (path == null) {
            return null;
        } else {
            File file = new File(path);
            return !file.isDirectory() && !file.mkdirs() ? null : file;
        }
    }
}
